package com.tt.lcd.qunyangGPRS.chanfangActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tt.lcd.qunyangGPRS.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Chanfang_Info extends Activity {
	private TextView zhanshu, pigs, add, chaxun, dangtian;
	ListView listView;
	IntentFilter intf;

	private BroadcastReceiver br = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getStringExtra("key2").equals("OK")) {
				onResume();
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		intf = new IntentFilter("com.lcd.qyRec");// 参数过滤器筛选条件，可以理解为广播名称
		registerReceiver(br, intf); // 注册广播
		setContentView(R.layout.chanfang_info);
		zhanshu = (TextView) findViewById(R.id.zhanshu);
		pigs = (TextView) findViewById(R.id.muzhushu);
		add = (TextView) findViewById(R.id.qy_add);
		chaxun = (TextView) findViewById(R.id.qy_chaxun);
		chaxun.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(Chanfang_Info.this, "执行查询，等待回复", 4).show();
				;
//				Register.getRegister().server.send((byte) 0x32);
			}
		});

		dangtian = (TextView) findViewById(R.id.qy_dangtain);
		dangtian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Chanfang_Info.this, CF_dangtian_info.class);

				startActivity(it);
			}
		});

		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent();
				it.setClass(Chanfang_Info.this, AddFeeder.class);

				startActivity(it);
			}
		});

		listView = (ListView) this.findViewById(R.id.date_list);

		zhanshu.setText("44");

		pigs.setText("545");

		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> data1 = new HashMap<String, Object>();
		data1.put("date", "2017-1-2");
		data1.put("shuliang", "23");
		data.add(data1);

//		try {
//			data = new DataManager().getDateInfo();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}

		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.chanfan_datelist,
				new String[] { "date", "shuliang" }, new int[] {
				R.id.date, R.id.shuliang });
		// 实现列表的显示
		listView.setAdapter(adapter);
		// 条目点击事件
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {
				ListView listView = (ListView) parent;
				HashMap<String, Object> data = (HashMap<String, Object>) listView
						.getItemAtPosition(position);
				String date = data.get("date").toString();

				Intent it = new Intent();
				it.setClass(Chanfang_Info.this, CF_siweiqi_info.class);

				// 下面可替换为
				// it.putExtra("str","字符串测试");
				// it.putExtra("int",30);
				Bundle bundle = new Bundle();
				bundle.putString("date", date);
				it.putExtras(bundle);

				startActivity(it);

			}

		});

	}

	@Override
	protected void onResume() {
		super.onResume();

//		zhanshu.setText("" + new DataManager().getZhanShu());
//
//		pigs.setText("" + new DataManager().getPigCount());
//
//		// 创建SimpleAdapter适配器将数据绑定到item显示控件上
//		List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
//		try {
//			data = new DataManager().getDateInfo();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//
//		SimpleAdapter adapter = new SimpleAdapter(this, data,
//				R.layout.date_info,
//				new String[] { "date", "shuliang", "muzhu" }, new int[] {
//						R.id.date, R.id.shuliang, R.id.pig_shuliang1 });
//		// 实现列表的显示
//		listView.setAdapter(adapter);
//		// 条目点击事件
//		listView.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				ListView listView = (ListView) parent;
//				HashMap<String, Object> data = (HashMap<String, Object>) listView
//						.getItemAtPosition(position);
//				String date = data.get("date").toString();
//
//				Intent it = new Intent();
//				it.setClass(Chanfang_Info.this, CF_siweiqi_info.class);
//
//				// 下面可替换为
//				// it.putExtra("str","字符串测试");
//				// it.putExtra("int",30);
//				Bundle bundle = new Bundle();
//				bundle.putString("date", date);
//				it.putExtras(bundle);
//
//				startActivity(it);
//
//			}
//
//		});

	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(br);
		super.onDestroy();
	}

}
